import { Entity, Column, Index, ManyToOne, JoinColumn } from 'typeorm';
import { BaseEntity } from '../common/base/base.entity';
import { Document } from './document.entity';
@Entity('chapters')
@Index('idx_document', ['documentId', 'chapterNumber'])
export class Chapter extends BaseEntity {
  @Column({
    type: 'bigint',
    comment: '文档ID',
  })
  documentId: string;

  @Column({
    type: 'varchar',
    length: 200,
    comment: '章节标题',
  })
  title: string;

  @Column({
    type: 'longtext',
    comment: '章节内容(Markdown)',
  })
  content: string;

  @Column({
    type: 'int',
    comment: '章节序号',
  })
  chapterNumber: number;

  @Column({
    type: 'boolean',
    default: false,
    comment: '是否免费试看',
  })
  isFree: boolean;

  @Column({
    type: 'enum',
    enum: ['draft', 'published'],
    default: 'draft',
    comment: '状态',
  })
  status: 'draft' | 'published';

  @Column({
    type: 'int',
    default: 0,
    comment: '字数',
  })
  wordCount: number;

  // 关联
  @ManyToOne(() => Document, (document) => document.chapters)
  @JoinColumn({ name: 'documentId' })
  document: Document;
}
